function f = LL_MTO_simple(x)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%THIS FILE IS THE LIKELIHOOD FOR A SIMPLE ORDERED PROBIT MODEL 
%USED TO FIND A STARTING VALUE FOR THE FULL MODEL ESTIMATED IN 
%"Evidence of Neighborhood Effects from Moving to Opportunity: 
%     LATEs of Neighborhood Quality"
%by Dionissi Aliprantis and Francisca G.-C. Richter
%SEE THE INCLUDED readme.txt FILE FOR AN EXPLANATION OF THIS FILE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%IMPORTING DATA AND LABELING VARIABLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent data;
if isempty(data)
    data=importdata('C:\MTO_1\matlab\matlab_data.txt');
end

%t=cputime;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DROP OBSERVATIONS WITH ANY MISSING VARIABLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data( any(data==-9,2), : ) = [];

%%%%%%%%%%%%%%%%
%LABEL VARIABLES
%%%%%%%%%%%%%%%%
D10         =data(:,1);     %D \in {1, 2, ..., 10}
HHnoteens   =data(:,2);     %X_1 
HHnofam     =data(:,3);     %X_2 
HHvictim    =data(:,4);     %X_3 
basequal    =data(:,5);     %X_4 
sitedummy2  =data(:,6);     %BOSTON  SITE DUMMY
sitedummy3  =data(:,7);     %CHICAGO SITE DUMMY
sitedummy4  =data(:,8);     %LA      SITE DUMMY
sitedummy5  =data(:,9);     %NYC     SITE DUMMY
exp         =data(:,10);    %INDICATOR FOR MTO EXPERIMENTAL VOUCHER 
S8          =data(:,11);    %INDICATOR FOR SECTION 8 VOUCHER
weight      =data(:,12);    %WEIGHTS
mover       =data(:,13);    %INDICATOR FOR MOVE WITH VOUCHER

N=length(D10);              %NUMBER OF OBSERVATIONS
T=2;                        %NUMBER OF TYPES (t=1 for S8, t=2 for Experimental)

C=zeros(N,10);               %COST CUTOFFS
gamma8=zeros(N,10);          %EFFECT OF VOUCHER INCLUDING UNOBSERVED HETEROGENEITY
gamma1=zeros(N,10);          %EFFECT OF VOUCHER INCLUDING UNOBSERVED HETEROGENEITY
index=zeros(N,10);           %LATENT INDEX

Pr=ones(N,1);               %PROBABILITY OF OBSERVATION i
LL=ones(N,1);               %LOG-LIKELIHOOD CONTRIBUTION OF OBSERVATION i

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CREATING LATENT INDEX COMPONENT BY COMPONENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%
%mu(X)
%%%%%%
bHHnoteens   =x(1);
bHHnofam     =x(2);
bHHvictim    =x(3);
bbasequal    =x(4);
bsitedummy2  =x(5);
bsitedummy3  =x(6);
bsitedummy4  =x(7);
bsitedummy5  =x(8);

mu=zeros(N,1);

mu(:)=bHHnoteens*HHnoteens(:) +bHHnofam*HHnofam(:) +bHHvictim*HHvictim(:) +bbasequal*basequal(:) ...
    +bsitedummy2*sitedummy2(:) +bsitedummy3*sitedummy3(:) +bsitedummy4*sitedummy4(:) +bsitedummy5*sitedummy5;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%COST FUNCTION (ie, CUTOFFS) 
%COST OF GOING FROM j TO j+1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bcutpoint(1)   =x(9);
bcutpoint(2)   =x(10);
bcutpoint(3)   =x(11);
bcutpoint(4)   =x(12);
bcutpoint(5)   =x(13);
bcutpoint(6)   =x(14);
bcutpoint(7)   =x(15);
bcutpoint(8)   =x(16);
bcutpoint(9)   =x(17);

%should be index=-Inf --> C(:,1)=-Inf;
for j=1:9
    C(:,j)=bcutpoint(j);
end
C(:,10)=Inf;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%COST-REDUCTION FROM SECTION 8 VOUCHERS (FOR HIGH-TYPES)
%COST-REDUCTION OF GOING FROM j TO j+1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%RECALL THAT gamma8(2) is the added benefit (/reduced cost) of moving from level 2 to level 3
%(so that gamma8(10) is irrelevant)
g8cutpoint(1)    =x(18);
g8cutpoint(2)    =x(19);
g8cutpoint(3)    =x(20);
g8cutpoint(4)    =x(21);
g8cutpoint(5)    =x(22);
g8cutpoint(6)    =x(23);
g8cutpoint(7)    =x(24);
g8cutpoint(8)    =x(25);
g8cutpoint(9)   =x(26);

g8sitedummy2     =x(27);
g8sitedummy3     =x(28);
g8sitedummy4     =x(29);
g8sitedummy5     =x(30);

gamma8=zeros(N,10);
for j=1:9
    gamma8(:,j)=g8cutpoint(j) +g8sitedummy2*sitedummy2(:) +g8sitedummy3*sitedummy3(:) ...
                                           +g8sitedummy4*sitedummy4(:) +g8sitedummy5*sitedummy5(:);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%COST-REDUCTION FROM EXPERIMENTAL VOUCHERS (FOR HIGH-TYPES)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g1cutpoint(1)   =x(31);
g1cutpoint(2)   =x(32);
g1cutpoint(3)   =x(33);
g1cutpoint(4)   =x(34);
g1cutpoint(5)   =x(35);
g1cutpoint(6)   =x(36);
g1cutpoint(7)   =x(37);
g1cutpoint(8)   =x(38);
g1cutpoint(9)   =x(39);

g1sitedummy2  =x(40);
g1sitedummy3  =x(41);
g1sitedummy4  =x(42);
g1sitedummy5  =x(43);

gamma1=zeros(N,10);
for j=1:9
    gamma1(:,j)=g1cutpoint(j) +g1sitedummy2*sitedummy2(:) +g1sitedummy3*sitedummy3(:) ...
                                           +g1sitedummy4*sitedummy4(:) +g1sitedummy5*sitedummy5(:);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%MARGINAL BENEFIT (ie, LATENT INDEX) OF INDIVIDUAL n FROM MOVING FROM LEVEL j TO LEVEL j+1 
%     (mu + gamma8_j^H*Z8 + gamma1_j^H*Z1 - C_j - V)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:10
    index(:,j)=mu(:)+exp(:).*gamma1(:,j)+S8(:).*gamma8(:,j)-C(:,j);
end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%CREATING LOG-LIKELIHOOD FUNCTION
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PROBABILITY OF OBSERVATION n CONDITIONAL ON PARAMETERS FOR ALL j
%Pr(D=j) = F_V(mu + gamma8_{j}^H*Z8 + gamma1_{j-1}^H*Z1 - C_{j-1}) 
%         - F_V(mu + gamma8_{j+1}^H*Z8 + gamma1_{j}^H*Z1 - C_{j}) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Prob=zeros(N,10);
%INFINITE BENEFIT FROM MOVING FROM D=0 TO D=1
    Prob(:,1)=double(1                    -normcdf(index(:,1))).*(D10(:) == 1);  
for j=2:10
    Prob(:,j)=double(normcdf(index(:,j-1))-normcdf(index(:,j))).*(D10(:) == j); 
end   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PROBABILITY OF OBSERVATION n CONDITIONAL ON PARAMETERS FOR SELECTED j
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Pr=zeros(N,1);    
for j=1:10
    Pr(:)=Pr(:)+Prob(:,j).*(D10(:) == j);
end

LL(:)=double(log(max(0.0000001,Pr(:)))).*weight(:);

f=double(-sum(LL(:)));
